package com.sitechasia.webx.components.xtable.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import com.sitechasia.webx.core.exception.BaseUnCheckedException;


public class Page extends com.sitechasia.webx.core.support.Page
    implements Serializable
{

    public Page()
    {
        this(0, 0L, Page.DEFAULT_PAGE_SIZE, ((List) (new ArrayList())));
    }

    public Page(int pageSize)
    {
        this(0, 0L, pageSize, ((List) (new ArrayList())));
    }

    public Page(int start, long totalSize, int pageSize, List data)
    {
        if(pageSize <= 0 || start < 0 || totalSize < 0L)
        {
            throw new BaseUnCheckedException("Illegal Arguments to Initiate Page Object");
        } else
        {
            this.pageSize = pageSize;
            this.start = start;
            totalCount = totalSize;
            this.data = data;
            return;
        }
    }

    public long getTotalCount()
    {
        return totalCount;
    }

    public long getTotalPageCount()
    {
        return totalCount % (long)pageSize != 0L ? totalCount / (long)pageSize + 1L : totalCount / (long)pageSize;
    }

    public int getPageSize()
    {
        return pageSize;
    }

    public void setResult(List data)
    {
        this.data = data;
    }

    public List getResult()
    {
        return data;
    }

    public int getCurrentPageNo()
    {
        return start / pageSize + 1;
    }

    public boolean hasNextPage()
    {
        return (long)getCurrentPageNo() < getTotalPageCount();
    }

    public boolean hasPreviousPage()
    {
        return getCurrentPageNo() > 1;
    }

    public boolean isEmpty()
    {
        return data == null || data.isEmpty();
    }

    public int getStartIndex()
    {
        return (getCurrentPageNo() - 1) * pageSize;
    }

    public int getEndIndex()
    {
        int endIndex = getCurrentPageNo() * pageSize - 1;
        return (long)endIndex < totalCount ? endIndex : (int)totalCount - 1;
    }

    protected static int getStartOfPage(int pageNo)
    {
        return getStartOfPage(pageNo, 20);
    }

    public static int getStartOfPage(int pageNo, int pageSize)
    {
        return (pageNo - 1) * pageSize;
    }

    private static final long serialVersionUID = 2728437513842150372L;
    private static final int DEFAULT_PAGE_SIZE = 10;
    private int pageSize;
    private int start;
    private List data;
    private long totalCount;
}

